package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccStockOutbound;
import com.ys.entity.storage.TccStockOutboundItem;
import com.ys.entity.storage.TccStockOutbound;
import com.ys.entity.storage.TccStockOutboundItem;
import com.ys.service.storage.TccStockOutboundService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/storage/tccStockOutbound")
public class WebTccStockOutboundController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccStockOutboundController.class);

	@Autowired
	private CommonApiService<TccStockOutbound> commonApiService;

	//菜单id
	private Long menuId = MenuId.TCC_StockOutbound;

	@Autowired
	private YspTaskService yspTaskService;

	@Autowired
	private TccStockOutboundService tccStockOutboundService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
	  try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);

		  TccStockOutbound entity = new TccStockOutbound();
            //关联数据
            String bid = commonApiService.getCommonNextBid(menuId);
            entity.setBid(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId, new ArrayList<Map<String, Object>>());
            return commonApiService.toAdd(model, entity, menuId, null, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
		try {
			TccStockOutbound tccStockOutbound = new TccStockOutbound().setBid(bid).queryFirst();
			if (tccStockOutbound == null){
				return W.error(model, "库存出库单不存在");
			}
			//主表信息
			Map<String, Object> extraParam = tccStockOutboundService.getExtraParam(bid);
			//子表信息
			List<Map<String, Object>> itemList = tccStockOutboundService.getItemList(bid);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

//			//是否校验 1是、0否
//			extraParam.put("isCheck", 1);

			//子表按钮
			Integer state = yspTaskService.flowState(bid);
			if (state == Flow.State.FINISH.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}else if (state == Flow.State.PROCESS.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}

			return commonApiService.toEdit(model,Long.parseLong(extraParam.get("id").toString()), new TccStockOutbound(), menuId, extraParam, subGridConfig);
		}catch (Exception e) {
			logger.error("编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

}